<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            size="large"
            @click="handleAdd"
            v-hasPermi="['task:tasks:add']"
        >创建任务
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            size="large"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['task:tasks:remove']"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="收集名称" align="center" prop="taskName"/>
      <el-table-column label="收集描述" align="center" prop="description"/>
      <el-table-column label="创建人" align="center" prop="createName" width="120"/>
      <el-table-column label="开始时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止时间" align="center" prop="deadline" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.deadline, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template #default="scope">
          {{ scope.row.status == 0 ? '进行中' : '已经结束' }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px">
        <template #default="scope">

          <el-button
              size="large"
              type="text"
              @click="editExport"
          >导出格式设置
          </el-button>
          <el-button
              size="large"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['task:tasks:edit']"
          >修改收集信息
          </el-button>
          <el-button
              size="large"
              type="text"
              icon="el-icon-delete"
              @click="this.$router.push('/task/taskdeal/index/' + scope.row.taskId)"
          >处理收集任务
          </el-button>

        </template>
      </el-table-column>

    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改任务创建对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="tasksRef" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
              v-model="form.createdAt"
              type="datetime"
              placeholder="开始时间"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD h:m:s"
          />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
              v-model="form.deadline"
              type="datetime"
              placeholder="开始时间"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD h:m:s"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="设置导出文件命名格式" v-model="editExport" width="600px">
        <el-form>
          <el-form-item label="可以选择的导出格式">
             学号、姓名、班级、收集名称
          </el-form-item>
          <el-form-item label="配置文件名称">
            学号、姓名、班级、收集名称
          </el-form-item>
          <el-form-item label="例如">
            学号_姓名_班级_收集名称 <br>
            收集名称_学号_姓名 <br>
            学号_收集名称 <br>等等...
          </el-form-item>
        </el-form>
    </el-dialog>
  </div>
</template>

<script setup name="Tasks">
import {listTasks, getTasks, delTasks, addTasks, updateTasks} from "@/api/task/tasks";
import {computed, getCurrentInstance, reactive, ref, toRefs} from "vue";

const {proxy} = getCurrentInstance();

const tasksList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
  form: {
    createdAt: '',
    deadline: ''
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    description: null,
    createdBy: null,
    createdAt: null,
    deadline: null,
    status: null
  },
  rules: {
    taskName: [
      {required: true, message: "任务名称不能为空", trigger: "blur"}
    ],
    description: [
      {required: true, message: "描述不能为空", trigger: "blur"}
    ],
    createdBy: [
      {required: true, message: "id谁创建的不能为空", trigger: "blur"}
    ],
    createdAt: [
      {required: true, message: "创建时间不能为空", trigger: "blur"}
    ],
    deadline: [
      {required: true, message: "截止日期不能为空", trigger: "blur"}
    ],
    status: [
      {required: true, message: "状态不能为空", trigger: "change"}
    ]
  }
});
const exportopen = ref(false)
const {queryParams, form, rules} = toRefs(data);


const editExport = () => {

  title.value = '设置导出格式'
  exportopen.value = true
}


/** 查询任务创建列表 */
function getList() {
  loading.value = true;
  listTasks(queryParams.value).then(response => {
    tasksList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    taskId: null,
    taskName: null,
    description: null,
    createdBy: null,
    createdAt: null,
    deadline: null,
    status: null
  };
  proxy.resetForm("tasksRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.taskId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加任务创建";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _taskId = row.taskId || ids.value
  getTasks(_taskId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改任务创建";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tasksRef"].validate(valid => {
    if (valid) {
      if (form.value.taskId != null) {
        updateTasks(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTasks(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _taskIds = row.taskId || ids.value;
  proxy.$modal.confirm('是否确认删除任务创建编号为"' + _taskIds + '"的数据项？').then(function () {
    return delTasks(_taskIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('task/tasks/export', {
    ...queryParams.value
  }, `tasks_${new Date().getTime()}.xlsx`)
}

getList();



</script>

